//
//  ZSHostManageViewController.m
//  ZhuShou
//
//  Created by Dejun Liu on 15/11/2.
//  Copyright © 2015年 jw. All rights reserved.
//

#import "ZSHostManageTableViewController.h"
#import "ZSNoneTableViewCell.h"
#import "BRAddHostViewController.h"
#import "ZSHostInfoViewController.h"

@interface ZSHostManageTableViewController ()

@property (nonatomic,strong) NSMutableArray *list;
@property (nonatomic, assign) int preSelectIndex;

@end

@implementation ZSHostManageTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.list =[NSMutableArray arrayWithArray:@[@{
                      @"title":@"主机1",
                      @"value":@"1"
                      },
                  @{
                      @"title":@"主机1",
                      @"value":@""
                      },
                  @{
                      @"title":@"主机1",
                      @"value":@""
                      },
                  @{
                      @"title":@"主机1",
                      @"value":@""
                      }
                  ]];
//    [self.tableView registerNib:[UINib nibWithNibName:@"ZSNoneTableViewCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestHosts:)];
        self.tableView.header = header;
    //
    //    MJRefreshAutoStateFooter *footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestData:)];
    //    self.tableView.footer = footer;
    //
    //    [self.tableView.header beginRefreshing];
    [self br_addRightEditingItem];
}

- (void)br_editHandle:(UIBarButtonItem *)item
{
    [super br_editHandle:item];
    NSInteger tag = item.tag;
    if (tag) {
        [self br_addNewButton:YES btnTitle:@"添加主机"];
        [self.tableView setEditing:YES];
    }else
    {
        [self br_addNewButton:NO btnTitle:@"添加主机"];
        [self.tableView setEditing:NO];
    }
}

- (void)br_newButtonHandle:(UIButton *)button
{
    [self br_pushViewControllerWithClassName:@"BRAddHostViewController"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return 1;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    NSDictionary *dic = [self.list objectAtIndex:indexPath.row];
    cell.textLabel.text = [dic objectForKey:@"title"];
    cell.imageView.image = [UIImage imageNamed:@"icon_curtain"];
//    cell.detailTextLabel.text = [dic objectForKey:@"value"];
    if ([[dic objectForKey:@"value"] intValue]) {
        
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (UITableViewCellEditingStyleDelete == editingStyle) {
        [self br_alertViewConfirmWithTitle:@"请确认是否删除此主机?" content:@" " operationIndex:^(NSInteger clickedIndex) {
            if (1 == clickedIndex) {
                if ([self.list[indexPath.row][@"value"] intValue]) {
                    [self br_AutoDisappearWithText:@"选定的主机不能被删除"];
                    return;
                }
                [self.list removeObjectAtIndex:indexPath.row];
                [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }
        }];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCellEditingStyle style;
    if (tableView.editing) {
        style = UITableViewCellEditingStyleDelete;
    }
    else
    {
        style = UITableViewCellEditingStyleDelete;
    }
    return style;
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = self.list[indexPath.row];
    
    if (indexPath.row == self.preSelectIndex) {
        // 显示主机信息
        ZSHostInfoViewController *vc = [[ZSHostInfoViewController alloc] init];
        vc.dic = dic;
        [self.navigationController pushViewController:vc animated:YES];
        
    } else {
     // 主机切换
        [self br_alertViewConfirmWithTitle:@"确认切换主机" content:@" " operationIndex:^(NSInteger clickedIndex) {
            if (1 == clickedIndex) {
#warning todo 主机切换请求，id之类参数
                // 成功显示
//                [self br_AutoDisappearWithText:@"主机切换成功!"];
//                self.preSelectIndex = indexPath.row;
                // 失败
                [self br_alertViewWithTitle:@"主机切换失败!" content:@"请检查主机网络连接是否正常" operationIndex:^(NSInteger clickedIndex) {
                }];
            }
        }];
    
    }
    
}

#pragma mark RequestData

- (void)requestData:(MJRefreshHeader *) commponent
{
    
}

- (void)requestHosts:(MJRefreshHeader *) commponent
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params hs_setSafeValue:@"mainframe" forKey:@"type"];
    [params hs_setSafeValue:@"email" forKey:@"name"];
    [params hs_setSafeValue:@"cheneylew@163.com" forKey:@"value"];
    
    [self br_progressHUD:YES];
    [BRServiceCenter BRServiceCenterUPDWithAction:@"select" CMD:[BRCommand CommandDeviceCRUD] params:params response:^(BRMessageResponseType type, NSInteger retryCount, id responseObj) {
        if (responseObj) {
            [self br_progressHUD:NO];
            BRUDPResponse *response = responseObj;
            if ([response.state isEqualToString:@"0000"]) {
                [self br_AutoDisappearWithText:response.stateDesc];
            }else
            {
                [self br_AutoDisappearWithText:response.stateDesc];
            }
        }
        
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        [self.tableView.footer noticeNoMoreData];
    }];
}

@end
